package com.wctj.api.business.common.manager.bo;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * SpringSecurity需要的用户信息封装类
 * @author carol
 */
public class AdminUserDetails implements UserDetails{
    /**
     * 后台用户
     */
    private AdminUserDTO adminUserDTO;
    /**
     * 拥有资源列表
     */
    private List<IzsAuthDTO> authList;

    public AdminUserDetails(AdminUserDTO adminUserDTO) {
        this.adminUserDTO = adminUserDTO;
    }

    public AdminUserDetails(AdminUserDTO adminUserDTO, List<IzsAuthDTO> authList) {
        this.adminUserDTO = adminUserDTO;
        this.authList = authList;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        // 返回当前用户所拥有的资源
        return authList.stream()
                .map(resource ->new SimpleGrantedAuthority(resource.getId()+":"+resource.getName()))
                .collect(Collectors.toList());
    }

    @Override
    public String getPassword() {
        return adminUserDTO.getPwd();
    }

    @Override
    public String getUsername() {
        return adminUserDTO.getUn();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return adminUserDTO.getStatus().equals(1);
    }
}
